import 'dart:math';

import 'package:first_flutter_project/main/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../AfterLayoutRoute.dart';
import '../Gesture/GestureDetectorConflictTest.dart';
import '../Gesture/GestureDetectorHitTest.dart';
import '../Gesture/NotificationInterruptTest.dart';

///组合动画用法
///
///抽奖滚动动画 stack的组合
///

class RollingLotteryStackAnimator extends StatefulWidget {
  @override
  _RollingLotteryStackAnimatorState createState() =>
      _RollingLotteryStackAnimatorState();
}

class _RollingLotteryStackAnimatorState
    extends State<RollingLotteryStackAnimator> with TickerProviderStateMixin {
  late AnimationController _controller;
  late Animation animation;
  ScrollController _scrollController = new ScrollController();

  var _containWidth = 300.0;
  var _containHeight = 100.0;
  var _listCount = 10;

  int currentScrollIndex = 0;
  int currentNextScrollIndex = 0;

  double currentDuring = 1;
  double lastScroll = 0;

  //手指向上滑，view向下滚
  bool scrollDown = true;

  //stack布局
  int stackCurrentScrollIndex = 0;
  double stackCurrentDuring = 1;
  double stackLastScroll = 0;

  @override
  void initState() {
    super.initState();

    // _controller =
    //     AnimationController(duration: Duration(seconds: 4), vsync: this);
    // animation = CurvedAnimation(
    //     parent: _controller, curve: Cubic(0.25, 0.5, 0.75, 1.0));
    //
    // _controller.forward();
    // animation.addListener(() => setState(() {}));

    // _controller.addStatusListener((status) {
    //   if (status == AnimationStatus.completed)
    //     _controller.reverse(from: 400);
    //   else if (status == AnimationStatus.dismissed) _controller.forward();
    // });

    WidgetsBinding.instance?.addPostFrameCallback((_) {
      lastScroll = 25;
      _scrollController.jumpTo(25);
    });

    _scrollController.addListener(() {
      print("滚动${_scrollController.offset}");
      if (_scrollController.offset > lastScroll) {
        scrollDown = true;
      } else {
        scrollDown = false;
      }

      currentScrollIndex = _scrollController.offset ~/ 33;
      print("滚动号：$currentScrollIndex");

      currentDuring = _scrollController.offset % 33 / 33;
      print("滚动进度：${currentDuring}");

      if (currentDuring >= 0.5) {
        currentNextScrollIndex = currentScrollIndex + 1;
      }

      setState(() {});
    });
  }

  double next(int min, int max) {
    double res = min + Random().nextInt(max - min).toDouble();
    print("tgw滚动指定到：$res");
    return res;
  }

  void onPressed() {
    var scrollPath = next(0, 30 * 50 - 100);
    if (scrollPath < 25) {
      scrollPath = 25;
    } else if (scrollPath >= 25) {
      var ban = scrollPath % 25;
      if (ban <= 12.5) {
        scrollPath = scrollPath - ban;
        if (scrollPath % 50 == 0) {
          scrollPath = scrollPath + 25;
        }
      } else {
        scrollPath = scrollPath - ban + 25;
        if (scrollPath % 50 == 0) {
          scrollPath = scrollPath + 25;
        }
      }
    }
    print("tgw滚动指定到最终：$scrollPath");
    _scrollController.animateTo(scrollPath,
        duration: Duration(seconds: 5), curve: Cubic(0.8, 0.8, 0.8, 1));
  }

  _playAnimation() async {
    try {
      //先正向执行动画
      await _controller.forward().orCancel;
      //再反向执行动画
      await _controller.reverse().orCancel;
    } on TickerCanceled {
      // the animation got canceled, probably because we were disposed
    }
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Center(
        child: Container(
          child: Column(
            children: [
              Stack(
                children: [
                  // Container(
                  //   child: Image.network(
                  //     "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi-1.lanrentuku.com%2F2020%2F12%2F3%2F7e21f352-c331-432b-9d14-19550fdcdb84.jpg%3FimageView2%2F2%2Fw%2F500&refer=http%3A%2F%2Fi-1.lanrentuku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656071301&t=8e6bfe07e4e9d5a7d5eb922b97995dec",
                  //     width: 300,
                  //     fit: BoxFit.fill,
                  //     height: 100,
                  //   ),
                  //   width: 300,
                  //   height: 100,
                  //   // color: Color(0xFF0AD5B8),
                  // ),

                  Container(
                    child: SingleChildScrollView(
                        controller: _scrollController,
                        // physics: new AlwaysScrollableScrollPhysics(),
                        // padding: EdgeInsets.only(top: 600,bottom: 600),

                        child: Stack(
                          children: initWidgets1(),
                        )),
                    width: _containWidth,
                    height: _containHeight,
                    // color: Color(0xFF0AD5B8),
                  ),
                  // Container(
                  //   width: 300,
                  //   height: 100,
                  //   color: Color(0xffffff),
                  // ),
                ],
              ),
              Transform(
                // transform:Matrix4.identity()
                //   ..translate(10.0, 10.0, 20.0),

                  transform:Matrix4.rotationX(0.9),
                child:               TextButton(onPressed: onPressed, child: Text("抽奖抽奖"))

              ),
            ],
          ),
          width: _containWidth,
          height: 500,
          color: Color(0x2CD21E1E),
        ),
      ),
    );
  }

  bool isSelected(int currentIndex, int currentScrollIndex) {
    return currentIndex == (currentScrollIndex + 1) ? true : false;
  }

  double checkTextSize(int currentIndex, int currentScrollIndex) {
    if (isSelected(currentIndex, currentScrollIndex)) {
      return 30;
    } else {
      return 20;
    }
  }

  double selectWidget(double currentDuring, int currentScrollIndex) {
    if (currentNextScrollIndex == currentScrollIndex) {
      return 24 + 6 * (currentDuring + 0.5);
    } else {
      return 30 - 6 * (currentDuring);
    }
    if (currentDuring <= 0.5) {
      return 24 + 6 * (currentDuring + 0.5);
    } else {
      return 30 - 6 * (currentDuring);
    }
  }

  double selectUpWidget(
      double currentDuring, int scrollIndex, int currentScrollIndex) {
    if (scrollIndex == currentScrollIndex) {
      return 24 - 6 * (currentDuring);
    } else {
      return 18 + 6 * (currentDuring);
    }
  }



  List<Widget> initWidgets1() {
    List<Widget> list = <Widget>[];
    for (int i = 0; i < 30; i++) {
      list.add(
        Container(
          // margin: EdgeInsets.only(top: (40 * i).toDouble()),
          child: Center(
            child: isSelected(i, currentScrollIndex)
                ? Container(
                    margin: EdgeInsets.only(
                        top: currentDuring>=0?(30*currentDuring).toDouble():40),
                    child: Center(
                      child: Text(
                        '落叶的位置，谱出$i',
                        style: TextStyle(
                            fontSize:
                                selectWidget(currentDuring, currentScrollIndex),
                            color: Color.fromARGB(
                                (255 *
                                        (currentDuring <= 0.5
                                            ? (0.5 + currentDuring)
                                            : 80))
                                    .toInt(),
                                255,
                                255,
                                255)),
                      ),
                    ))
                : Container(
              margin: currentDuring<=0?EdgeInsets.only(
                  top: (40*i).toDouble()): i==currentScrollIndex? EdgeInsets.only(
                  top: (30*currentDuring).toDouble()):EdgeInsets.all(
                  0.0),
              child:Center(
                    child: Text(
                      '落叶的位置，谱出一$i。',
                      style: TextStyle(
                          fontSize: selectUpWidget(
                              currentDuring, i, currentScrollIndex),
                          color: Color.fromARGB((255).toInt(), 255, 255, 255)),
                    ),
                  )),
          ),
          height: 33,
        ),
      );
    }
    return list;
  }
}

class ClipperPath extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return new Rect.fromLTRB(0, 10, size.width, size.height);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return true;
  }

// child: Column(
//   children: [
//     Container(
//         child: ClipRect(
//           child: Center(
//             child: ShaderMask(
//               shaderCallback: (Rect bounds) {
//                 return LinearGradient(
//                   begin: Alignment.topCenter,
//                   end: Alignment.bottomCenter,
//                   colors: <Color>[
//                     Color(0x27FFFFFF),
//                     Color(0xA6FFFFFF)
//                   ],
//                   tileMode: TileMode.mirror,
//                 ).createShader(bounds);
//               },
//               blendMode: BlendMode.srcATop,
//               child: Center(
//                 child: Text(
//                   '落叶的位置，谱出一首诗。',
//                   style: TextStyle(fontSize: 20),
//                 ),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: Center(
//             child: Text(
//               '落叶的位置。落叶',
//               style:
//                   TextStyle(fontSize: 30, color: Color(0xFFFFFFFF)),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//   ],
// ),
}
